package client;


import common.IEvents;
import java.awt.Rectangle;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import javax.swing.JPanel;
/**
 * @author windsea
 *
 */
class CommandSender implements KeyListener,
        MouseMotionListener,MouseListener, MouseWheelListener {

    private JPanel pScreen = null;
    private PrintWriter writer = null;
    private Rectangle screenSize = null;

    CommandSender(Socket client, JPanel p, Rectangle r) {
        pScreen = p;
        screenSize = r;
        pScreen.addKeyListener(this);
        pScreen.addMouseListener(this);
        pScreen.addMouseMotionListener(this);
        pScreen.addMouseWheelListener(this);
        try {
            writer = new PrintWriter(client.getOutputStream());
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        
    }


    @Override
    public void mouseDragged(MouseEvent e) {
    }

    @Override
    public void mouseMoved(MouseEvent e) {
        double xScale = screenSize.getWidth()/pScreen.getWidth();    
        double yScale = screenSize.getHeight()/pScreen.getHeight();       
        System.out.println("Mouse Moved");
        writer.println(IEvents.MOUSE_MOVE);
        writer.println((int)(e.getX() * xScale));
        writer.println((int)(e.getY() * yScale));
        writer.flush();
    }

    

    @Override
    public void mousePressed(MouseEvent e) {
        writer.println(IEvents.MOUSE_PRESSED);
        int button = e.getButton();
        switch (button) {
		case 1:	
			writer.println(InputEvent.BUTTON1_MASK);
			break;
		case 2:
			writer.println(InputEvent.BUTTON2_MASK);
			break;
		case 3:
			writer.println(InputEvent.BUTTON3_MASK);
			break;
		default:
			writer.println(InputEvent.BUTTON1_MASK);
			break;
		}                
        writer.flush();
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        writer.println(IEvents.MOUSE_RELEASED);
        int button = e.getButton();
        switch (button) {
		case 1:	
			writer.println(InputEvent.BUTTON1_MASK);
			break;
		case 2:
			writer.println(InputEvent.BUTTON2_MASK);
			break;
		case 3:
			writer.println(InputEvent.BUTTON3_MASK);
			break;
		default:
			writer.println(InputEvent.BUTTON1_MASK);
			break;
		}                
        writer.flush();
    }

    @Override
    public void keyPressed(KeyEvent e) {
        writer.println(IEvents.KEY_PRESSED);
        writer.println(e.getKeyCode());
        writer.flush();
    }

    @Override
    public void keyReleased(KeyEvent e) {
        writer.println(IEvents.KEY_RELEASED);
        writer.println(e.getKeyCode());
        writer.flush();
    }
    @Override
	public void mouseWheelMoved(MouseWheelEvent e) {
    	int unit = e.getUnitsToScroll();    	
    	writer.println(IEvents.MOUSE_WHEEL);
    	writer.println(unit);
    	writer.flush();
	}
    @Override
    public void mouseEntered(MouseEvent e) {
    }
    @Override
    public void mouseExited(MouseEvent e) {
    }
    @Override
    public void keyTyped(KeyEvent e) {
    }	
    @Override
    public void mouseClicked(MouseEvent e) {
    }
}
